package web;

import Utils.ReflectUtils;
import Utils.web.servlet.BaseServlet;
import Utils.web.servlet.ServletUtils;
import pojo.User;
import service.Impl.TaskServiceImpl;
import service.TaskService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

/**
 * @author d11yu
 */
@WebServlet(name = "ClientServlet", value = "/ClientServlet")
public class ClientServlet extends BaseServlet {
    private TaskService taskService = new TaskServiceImpl();

    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        ServletUtils.page(request, response, "ClientServlet?action=page", "/pages/client/index.jsp", taskService);
    }

    protected void pageByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, InvocationTargetException, IllegalAccessException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/pages/user/login.jsp");
        } else {
            ServletUtils.page(
                    request,
                    response,
                    "ClientServlet?action=pageByUserId",
                    "/pages/client/index.jsp",
                    taskService,
                    ReflectUtils.getMethod(taskService, "getPageByUserId", Integer.class, Integer.class, String.class, Integer.class),
                    user.getId()
            );
        }
    }

    protected void pageTasknameBlurByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, InvocationTargetException, IllegalAccessException {
        String taskname = request.getParameter("taskname");
        User user = (User) request.getSession().getAttribute("user");
        ServletUtils.page(
                request,
                response,
                "ClientServlet?action=pageTasknameBlurByUserId",
                "/pages/client/index.jsp",
                taskService,
                ReflectUtils.getMethod(taskService, "getPageByTasknameBlurByUserId", Integer.class, Integer.class, String.class,String.class, Integer.class),
                taskname,
                user.getId()
        );
    }
}
